package com.fz.admin.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fz.admin.mapper.GoodsMapper;
import com.fz.admin.mapper.OrderDetailMapper;
import com.fz.admin.mapper.OrderMapper;
import com.fz.admin.mapper.ProOrderMapper;
import com.fz.model.Goods;
import com.fz.model.Order;
import com.fz.model.OrderDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

/**
 * Created by webrx on 2017/7/19.
 */
@Controller
public class OrdDetail {
    @Autowired
    private OrderDetailMapper detail;
    @Autowired
    private ProOrderMapper order;

    @Autowired
    private GoodsMapper goods;

    @RequestMapping(value = "/fz_detail_query.do" ,produces = {"application/text;charset=utf-8"})

    public ModelAndView query(HttpServletRequest req){
        ModelAndView mv = new ModelAndView("/admin/page/order/orderDetail");
        String id =req.getParameter("id");
        System.out.println(id);
        int orderId =Integer.parseInt(id);

        List<Map<String,Object>> list = detail.queryBynum(orderId);
        for(Map<String,Object> de:list){
            int gid = Integer.parseInt(String.valueOf(de.get("gid")));
            String logo=goods.queryById(gid).getLogo();
            de.put("logo",logo);
        }
        mv.addObject("list",list);
        System.out.println(list.size());
      /*  List<Map<String,Object>> list = detail.queryAll();
        for(Map<String,Object> deta :list){
            int orderid = Integer.parseInt(String.valueOf(deta.get("order_id")));
           Order ord = order.selectById(orderid);
           String ordernum= ord.getOrderNum();
           deta.put("order_id",ordernum);
                   }*/
       // System.out.println(JSON.toJSONString(list));
        //return JSON.toJSONString(list);
        return mv;
    }

    @RequestMapping(value = "/fz_detail_insert.do", produces = {"application/text;charset=UTF-8"}) @ResponseBody
    public String  insert(HttpServletRequest req){
        String idd =req.getParameter("id");
        int id  =Integer.parseInt(idd);
        OrderDetail det = detail.selectById(id);
        return JSON.toJSONString(det);
    }
    @RequestMapping(value = "/fz_detail_update.do" ,produces = {"application/text;charset=utf-8"})
    public void update(HttpServletRequest req){
        String json = req.getParameter("data");
        JSONObject jso = (JSONObject) JSONObject.parse(json);
        System.out.println(jso);
        int id =jso.getInteger("id");
        int gid =jso.getInteger("gid");
        int orderId =jso.getInteger("orderId");
        Double price =jso.getDouble("price");
        int  num =jso.getInteger("num");
        String name =jso.getString("name");
        OrderDetail orderDetail= new OrderDetail();
        System.out.println(jso.get("num"));
    }
}
